package com.cx.thread.test;

import java.util.LinkedList;

public class Consumer {
    private String name; // 消费者标识
    private LinkedList<String> buffer;// 消息集合
    public Consumer(String name, LinkedList<String> buffer) {
        this.name = name;
        this.buffer = buffer ;
    }

    /**
     * 拉取数据
     */
    public void pull() {
        while (true){
            /*
             *如果缓存的大小小于等于0，线程阻塞，否则，就需要唤醒所有线程。
             */
            synchronized (buffer) {
                try{
                    if(buffer.size() > 0){
                        System.out.println(name + "拉取消息:" + buffer.getFirst());
                        buffer.removeFirst();
                        buffer.notifyAll();
                    }else{
                        buffer.wait();
                        System.out.println(name + "线程阻塞");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}